package org.zch.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.zch.common.utils.ListUtils;
import org.zch.dao.RecommendDAO;
import org.zch.dao.WorksDAO;
import org.zch.entity.dto.BottomDTO;
import org.zch.entity.dto.WorksDTO;
import org.zch.entity.po.RecommendPO;
import org.zch.entity.po.WorksPO;
import org.zch.entity.vo.WorksVO;
import org.zch.service.RecommendService;
import org.zch.service.WorksService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 作品推荐表(Recommend)表服务实现类
 *
 * @author 刘新
 * @since 2024-01-05 14:43:12
 */
@Service("recommendService")
public class RecommendServiceImpl extends ServiceImpl<RecommendDAO, RecommendPO> implements RecommendService {

    @Autowired
    private RecommendDAO recommendDAO;

    @Autowired
    private WorksService worksService;




    /**
     * 推荐列表
     *
     * @param page
     * @return
     */
    @Override
    public IPage<WorksVO> recommendList(WorksDTO page) {
        IPage<WorksVO> iPage = recommendDAO.recommendList(page);
        //推荐列表垫底数数
        //1.有图
        //2.有文字
        //3.时间倒叙
        //如果此时还没有数据
        if (ArrayUtil.isAllNotEmpty(iPage)) {
            List<WorksVO> list = new ArrayList<>(iPage.getRecords());
            if (list.size() < 20) {
                Integer bottomSize = 20 - list.size();
                BottomDTO bottomDTO = new BottomDTO();
                bottomDTO.setSize(bottomSize);
                bottomDTO.setHaveImg(1);
                if (ArrayUtil.isAllNotEmpty(list)){
                    List<Integer> notList = list.stream().map(WorksPO::getId).collect(Collectors.toList());
                    bottomDTO.setNotList(notList);
                }
                List<WorksVO> bottom = worksService.getBottom(page.getUserCode(),  bottomDTO);
                list.addAll(bottom);
                iPage.setRecords(list);
                iPage.setTotal(list.size());
            }
            iPage.setRecords(list);
        }

        return iPage;

    }

    /**
     * 将作品设为推荐
     *
     * @param list 要推荐的作品
     * @return
     */
    @Override
    public Integer recommendWorks(List<RecommendPO> list) {
        for (RecommendPO recommendPO : list) {
            recommendPO.setDeleteFlag(0);
        }
        return this.recommendDAO.insertBatch(list);
    }

    /**
     * 根据作品id，删除推荐表数据
     *
     * @param id 作品id
     * @return
     */
    @Override
    public Integer deleteByWorksId(Integer id) {
        return recommendDAO.deleteByWorksId(id);
    }



}

